package main

import (
	"bytes"
	"fmt"
	"goPlay/img"
	"image"
	"image/draw"
	_ "image/gif"
	"image/jpeg"
	"image/png"
	"net/http"
)

func main() {
	fmt.Println("---------start--------")
	resp, err := http.Get("http://7xohdn.com2.z0.glb.qiniucdn.com/1.png")
	fmt.Println(err)
	defer resp.Body.Close()
	img1, format, err := image.Decode(resp.Body)
	// fmt.Println("img", fmt.Sprintf("%T", img), err, resp.Body)
	b := img1.Bounds()
	imgWidth := b.Max.X
	imgHeight := b.Max.Y
	var send_s3 []byte
	fmt.Println("w-h:", imgHeight, imgWidth)
	// f, _ := os.Create("testaaa.png") //创建文件
	// defer f.Close()                  //关闭文件
	// png.Encode(f, img1)              //写入文件
	if format == "jpeg" {
		fmt.Println("jpeg")
		buf := new(bytes.Buffer)
		err := jpeg.Encode(buf, img1, nil)
		send_s3 = buf.Bytes()
		fmt.Println("send_s3 jpg:", len(send_s3), err, imgWidth*imgHeight)
	} else if format == "png" {
		fmt.Println("png")
		buf := new(bytes.Buffer)
		err := png.Encode(buf, img1)
		send_s3 = buf.Bytes()
		fmt.Println("send_s3:", fmt.Sprintf("%T", send_s3), len(send_s3), err, imgWidth*imgHeight)
		// // buf_end := bytes.NewBuffer(send_s3)
		// // img_end, format_end, err := image.Decode(buf_end)
		// fmt.Println("img", fmt.Sprintf("%T", img_end), err, format_end)
	}
	fmt.Println("before in GetImgLineBySO", len(send_s3))

	size := img1.Bounds()
	pic := image.NewGray(size)
	fmt.Println("img1", img1.Bounds().Min, img1.Bounds().Max)
	draw.Draw(pic, size, img1, img1.Bounds().Min, draw.Src)
	// f, _ = os.Create("testgray.png") //创建文件
	// defer f.Close()                  //关闭文件
	// png.Encode(f, pic)               //写入文件
	// buf := new(bytes.Buffer)
	// err = png.Encode(buf, pic)
	// send_s3 = buf.Bytes()
	fmt.Println("end", len(pic.Pix), len(send_s3))
	// img.GetImgLineBySO(img1)

	img_data, format := img.GetImg("http://7xohdn.com2.z0.glb.qiniucdn.com/1.png")
	// get img data
	// so
	posdata := img.GetImgLine(img_data)
	// cut img data
	imgs := img.CutImg(img_data, posdata)
	fmt.Println("----------end---------", len(imgs))
}
